package com.wanban

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.wanban.adutils.AdUtils
import com.wanban.adutils.callback.OnLoadAdCallback
import com.wanban.adutils.db.bean.AdContent
import com.wanban.adutils.tools.AdConstants
import com.wanban.adutils.utils.*
import com.wanban.base.BaseActivity
import com.wanban.network.OnRetrofitResultListener
import com.wanban.network.SplashViewModel
import io.reactivex.disposables.Disposable

class SplashActivity : BaseActivity() {

    private var startTime: Long = 0L
    private var disposable: Disposable? = null
    private val viewModel by lazy { getViewModel(SplashViewModel::class.java) }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)
        startTime = System.currentTimeMillis()
        AdUtils.testIronSource(this)



        viewModel.checkApp(object: OnRetrofitResultListener<Boolean> {
            override fun onSuccess(t: Boolean) {
                doHttpAd()
            }

            override fun onFailed(msg: String) {

            }
        })
    }

    private fun doHttpAd() {
        viewModel.getAdList(object : OnRetrofitResultListener<List<AdContent>> {
            override fun onSuccess(t: List<AdContent>) {
                Log.d("MultipleAd", "splash -> 广告加载完成")
                // 预加载插屏广告
                // preLoadVideoDetailAd()
                var adId = t.firstOrNull { it.adId == AdConstants.AD_ID_OPEN_AD_V2 }?.adId?:0
                if (adId == 0L) {
                    toMain()
                    return
                }
                Log.e(AdConstants.TAG, "首页的开屏广告 -> 准备加载广告")
                OpenAdTools.loadAd(this@SplashActivity, adId, object : OnLoadAdCallback() {

                    override fun onAdLoaded(ad: Any?, vararg tag: Int) {
                        super.onAdLoaded(ad, *tag)
                        Log.e(AdConstants.TAG, "首页的开屏广告 -> 加载成功")
                        toMain()
                    }

                    override fun onAdLoadFailed(errorCode: Int) {
                        super.onAdLoadFailed(errorCode)
                        Log.e(AdConstants.TAG, "首页的开屏广告 -> 加载失败")
                        OpenAdTools.release()
                        toMain()
                    }
                })
            }

            override fun onFailed(msg: String) {

            }
        })
    }

    private fun preLoadVideoDetailAd() {
        VideoInterstitialAdTools.loadAd(this@SplashActivity, object : OnLoadAdCallback() {
            override fun onAdLoaded(ad: Any?, vararg tag: Int) {
                super.onAdLoaded(ad, *tag)
                Log.d(AdConstants.TAG, "预加载详情页广告加载完成")
//                isVideoLoadComplete = true
//                if (isReopenLoadComplete) {
//                    toMain()
//                }

                toMain()
            }

            override fun onAdLoadFailed(errorCode: Int) {
                super.onAdLoadFailed(errorCode)
                Log.d(AdConstants.TAG, "预加载详情页广告加载失败")
//                isVideoLoadComplete = true
//                if (isReopenLoadComplete) {
//                    toMain()
//                }

                toMain()
            }
        })
    }


    /**
     * 获取ViewModel
     */
    fun <T : ViewModel> getViewModel(modelClass: Class<T>): T {
        return ViewModelProvider(
            this,
            ViewModelProvider.AndroidViewModelFactory.getInstance(application)
        ).get(modelClass)
    }

    private fun toMain() {
        disposable?.dispose()
        val intent = Intent(this, MainActivity::class.java)
        startActivity(intent)
        finish()
    }


}
